//
//  YSCommonFlowNewViewController.m
//  YaSha-iOS
//
//  Created by YaSha_Tom on 2018/8/3.
//  Copyright © 2018年 亚厦装饰股份有限公司. All rights reserved.
//

#import "YSCommonFlowNewViewController.h"
#import "YSFlowAttachPageController.h"

@interface YSCommonFlowNewViewController ()

@end

@implementation YSCommonFlowNewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    _flowFormBottomView = [[YSFlowFormBottomView alloc] init];
    [_flowFormBottomView setCellModel:_cellModel withFlowType:_flowType];
    [self.view addSubview:_flowFormBottomView];
    [_flowFormBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-kBottomHeight);
        make.height.mas_equalTo(50*kHeightScale);
    }];
    
    [self checkTrans];
    //监听历程处理通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateFlowStatus:) name:@"PostUpdateFlowStatus" object:nil];
}
- (void)doNetworking {
    [QMUITips showLoadingInView:self.view];
    
}
#pragma mark - 流程状态改变通知
- (void)updateFlowStatus:(NSNotification *)notification {
    NSDictionary *handleDic = notification.userInfo;
    NSUInteger flowType = [handleDic[@"flowType"] integerValue];
    NSUInteger flowHandleType = [handleDic[@"flowHandleType"] integerValue];
    if (flowHandleType == FlowHandleTypeTrans) {//转阅要回到详情页，暂时只需要提示
        [QMUITips showSucceed:@"流程转阅成功" inView:self.view hideAfterDelay:1];
    }
}
/** 如果是他人转阅的流程，查看时不可操作，并且调用流程处理接口返回时并删除该条数据，
    知会节点时，点击待办进入详情页会调用流程处理<做审批处理>接口，待办列表消失
 */
- (void)checkTrans {
    if (_flowType == YSFlowTypeTodo) {
        if (_cellModel.turnRead) {
            [_flowFormBottomView removeFromSuperview];
            NSString *urlString = [NSString stringWithFormat:@"%@%@/%zd", YSDomain, flowProcessApi, FlowHandleTypeTurnRead+1];
            NSDictionary *payload = @{
                                      @"message": @"",
                                      @"taskId": _cellModel.taskId,
                                      @"userIds": @[],
                                      @"processInstanceId": _cellModel.processInstanceId
                                      };
            [YSNetManager ys_request_POSTWithUrlString:urlString isNeedCache:NO parameters:payload successBlock:^(id response) {
                if ([response[@"code"] intValue] == 1) {
                    NSDictionary *handleDic = @{
                                                @"flowType": [NSString stringWithFormat:@"%zd", _flowType],
                                                @"flowHandleType": [NSString stringWithFormat:@"%zd", FlowHandleTypeTurnRead],
                                                @"model": _cellModel
                                                };
                    //转阅已读  发通知改变状态
                    _cellModel.turnRead ? [[NSNotificationCenter defaultCenter] postNotificationName:@"PostUpdateFlowStatus" object:nil userInfo:handleDic] : nil;
                }
            } failureBlock:^(NSError *error) {
                DLog(@"error:%@", error);
            } progress:nil];
        }
        if ([_cellModel.taskType isEqualToString:FlowTaskZH]) {

            NSString *urlString = [NSString stringWithFormat:@"%@%@/%zd", YSDomain, flowProcessApi, FlowHandleTypeApproval+1];
            NSDictionary *payload = @{
                                      @"message": @"",
                                      @"taskId": _cellModel.taskId,
                                      @"userIds": @[],
                                      @"processInstanceId": _cellModel.processInstanceId
                                      };
            [YSNetManager ys_request_POSTWithUrlString:urlString isNeedCache:NO parameters:payload successBlock:^(id response) {
                if ([response[@"code"] intValue] == 1) {
                    NSDictionary *handleDic = @{
                                                @"flowType": [NSString stringWithFormat:@"%zd", _flowType],
                                                @"flowHandleType": [NSString stringWithFormat:@"%zd", FlowHandleTypeApproval],
                                                @"model": _cellModel
                                                };
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"PostUpdateFlowStatus" object:nil userInfo:handleDic];
                }
            } failureBlock:^(NSError *error) {
                DLog(@"error:%@", error);
            } progress:nil];
        }
    }
   
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 50*kHeightScale;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 50*kHeightScale)];
    return footerView;
}

/** 处理/转阅 */
- (void)monitorAction {
    YSWeak;
    [_flowFormBottomView.sendActionSubject subscribeNext:^(UIButton *button) {
        YSStrong;
        YSFlowHandleViewController *flowHandleViewController = [[YSFlowHandleViewController alloc] init];
        flowHandleViewController.cellModel = strongSelf.cellModel;
        if (button.tag == 0) {
            QMUIAlertAction *action0 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertAction *action) {
                
            }];
            QMUIAlertAction *action1 = [QMUIAlertAction actionWithTitle:@"审批" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
                flowHandleViewController.flowHandleType = FlowHandleTypeApproval;
                [strongSelf.navigationController pushViewController:flowHandleViewController animated:YES];
            }];
            QMUIAlertAction *action2 = [QMUIAlertAction actionWithTitle:@"驳回" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
                flowHandleViewController.flowHandleType = FlowHandleTypeReject;
                [strongSelf.navigationController pushViewController:flowHandleViewController animated:YES];
            }];
            QMUIAlertAction *action3 = [QMUIAlertAction actionWithTitle:@"审批并加签" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
                flowHandleViewController.flowHandleType = FlowHandleTypeAdd;
                [strongSelf.navigationController pushViewController:flowHandleViewController animated:YES];
            }];
            QMUIAlertAction *action4 = [QMUIAlertAction actionWithTitle:@"转办" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
                flowHandleViewController.flowHandleType = FlowHandleTypeChange;
                [strongSelf.navigationController pushViewController:flowHandleViewController animated:YES];
            }];
            QMUIAlertAction *action5 = [QMUIAlertAction actionWithTitle:@"暂存" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
                flowHandleViewController.flowHandleType = FlowHandleTypeSave;
                [strongSelf.navigationController pushViewController:flowHandleViewController animated:YES];
            }];
            QMUIAlertAction *action6 = [QMUIAlertAction actionWithTitle:@"评审" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
                flowHandleViewController.flowHandleType = FlowHandleTypeReview;
                [strongSelf.navigationController pushViewController:flowHandleViewController animated:YES];
            }];
            
            QMUIAlertAction *action7 = [QMUIAlertAction actionWithTitle:@"协同" style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
                flowHandleViewController.flowHandleType = YSFlowHandleTypeSynergy;
                [strongSelf.navigationController pushViewController:flowHandleViewController animated:YES];
            }];
            
            QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
            
            [alertController addAction:action0];
            
            if([strongSelf.cellModel.taskType isEqualToString:FlowTaskPS]){
                [alertController addAction:action6];
            }else if([strongSelf.cellModel.taskType isEqualToString:FlowTaskXT]){
                [alertController addAction:action7];
            }else{
                [alertController addAction:action1];
                [alertController addAction:action2];
                [alertController addAction:action3];
            }
            
            [alertController addAction:action4];
            [alertController addAction:action5];
            [alertController showWithAnimated:YES];
        } else if (button.tag == 2) {
            flowHandleViewController.flowHandleType = FlowHandleTypeRevoke;
            [strongSelf.navigationController pushViewController:flowHandleViewController animated:YES];
        } else {
            flowHandleViewController.flowHandleType = FlowHandleTypeTrans;
            [strongSelf.navigationController pushViewController:flowHandleViewController animated:YES];
            
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
